package com.tsingteng.cosfun.utils;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.ImageViewTarget;
import com.bumptech.glide.request.target.Target;
import com.tsingteng.cosfun.R;
import com.tsingteng.cosfun.common.Config;

import java.io.File;


/**
 * 描述:图片工具类
 * 创建人: 肖利
 *时间: 2018/7/30/5/22.
 * qq: (872179966)
 */
public class ImageUtils {
    /**
     * 常规使用
     * @param context   上下文
     * @param url       图片链接
     * @param imageView 目标view
     */
    public static void LoadImage(Context context, Object url, ImageView imageView) {
        Glide.with(context).load(url)
                .apply(new RequestOptions()
                        .centerCrop()
                        .apply(new RequestOptions().error(R.drawable.avatar_pic).placeholder(R.drawable.avatar_pic)))
                .into(imageView);
    }
    public static void LoadImage(Context context, Object url, ImageView imageView,int drawable) {
        Glide.with(context).load(url)
                .apply(new RequestOptions()
                        .centerCrop()
                        .apply(new RequestOptions().error(drawable).placeholder(drawable)))
                .into(imageView);
    }




    /**
     * 自定义RequestOptions使用
     * @param context        上下文
     * @param url            图片链接
     * @param requestOptions
     * @param imageView      目标view
     */
    public static void LoadImage(Context context, Object url, ImageView imageView, RequestOptions requestOptions) {
        Glide.with(context).load(url)
                .apply(requestOptions)
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageView);
    }

    /**
     * 自定义RequestOptions使用
     * @param fragment
     * @param url            图片链接
     * @param requestOptions
     * @param imageView      目标view
     */
    public static void LoadImage(Fragment fragment, Object url, ImageView imageView, RequestOptions requestOptions) {
        Glide.with(fragment).load(url)
                .apply(requestOptions)
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageView);
    }


    /**
     * 需要回调时使用
     * @param context         上下文
     * @param url             图片链接
     * @param imageViewTarget 回调需求
     */
    public static void LoadImage(Context context, Object url, ImageViewTarget imageViewTarget) {
        Glide.with(context).load(url)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .transition(new DrawableTransitionOptions().crossFade(800))
                .into(imageViewTarget);
    }

    /**
     * 需要回调时使用
     * @param context   上下文
     * @param url       图片链接
     * @param imageView 回调需求
     */
    public static void LoadImage(Context context, Object url, ImageView imageView, RequestListener listener) {
        Glide.with(context).load(url)
                //.thumbnail(0.1f)
                .apply(new RequestOptions()
                        .diskCacheStrategy(DiskCacheStrategy.ALL))
                .transition(new DrawableTransitionOptions().crossFade(800))
                .listener(listener)
                .into(imageView);
    }
    /**
     * 常规使用
     * @param context   上下文
     * @param url       图片链接
     * @param imageView 目标view
     */
    public static void LoadImagePlay(Context context, Object url, ImageView imageView) {
        Glide.with(context).load(url)
                .apply(new RequestOptions().placeholder(R.mipmap.cos_background)
                        .centerCrop()
                        .apply(new RequestOptions().error(R.mipmap.cos_background)))
                .into(imageView);
    }
    private static RequestOptions head_options = RequestOptions.placeholderOf(R.drawable.ic_launcher_background);


    /**
     *
     * @param context
     * @param url
     * @param imageView
     */
    public static void LoadUserVideoImage(Context context, Object url, ImageView imageView) {
        Glide.with(context).load(url)
                .apply(new RequestOptions()
                        .centerCrop()
                        .apply(new RequestOptions().error(R.drawable.video_load_failed).placeholder(R.drawable.video_load_failed)))
                .into(imageView);
    }


}
